package dev2dev.textclient.voip;

/**
 * @author jinbing.yao
 * @createTime 2023-10-14 15:38
 **/

import javax.sound.sampled.*;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class VoipSipAudioExample2 {

    private static final int SAMPLE_RATE = 8000; // 采样率
    private static final int BUFFER_SIZE = 1024; // 缓冲区大小

    public static void main(String[] args) {
        try {
            // 创建音频输入线程
            Thread audioInputThread = new Thread(new AudioInputRunnable());
            audioInputThread.start();

            // 创建音频输出线程
//            Thread audioOutputThread = new Thread(new AudioOutputRunnable());
//            audioOutputThread.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 音频输入线程
    static class AudioInputRunnable implements Runnable {

        @Override
        public void run() {
            try {
                // 获取音频输入设备
                AudioFormat audioFormat = new AudioFormat(SAMPLE_RATE, 16, 1, true, true);
                DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
                TargetDataLine targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
                targetDataLine.open(audioFormat);
                targetDataLine.start();

//                // 创建UDP套接字
//                DatagramSocket datagramSocket = new DatagramSocket();

                // 读取音频数据并发送
                byte[] buffer = new byte[BUFFER_SIZE];

                try {
                    VoipSipExample voipSipExample = new VoipSipExample("192.168.31.130", 6000);
                    voipSipExample.init();
                    while (true) {
                        targetDataLine.read(buffer, 0, buffer.length);
                        voipSipExample.makeCall("771", "192.168.31.130", "709", "192.168.31.101", buffer);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (LineUnavailableException e) {
                e.printStackTrace();
            }
        }
    }

    // 音频输出线程
    static class AudioOutputRunnable implements Runnable {

        @Override
        public void run() {
            try {
                // 获取音频输出设备
                AudioFormat audioFormat = new AudioFormat(SAMPLE_RATE, 16, 1, true, true);
                DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
                SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
                sourceDataLine.open(audioFormat);
                sourceDataLine.start();

                // 创建UDP套接字
                DatagramSocket datagramSocket = new DatagramSocket(5000);

                // 接收音频数据并播放
                byte[] buffer = new byte[BUFFER_SIZE];
                while (true) {
                    DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length);
                    datagramSocket.receive(datagramPacket);
                    sourceDataLine.write(datagramPacket.getData(), 0, datagramPacket.getLength());
                }
            } catch (LineUnavailableException | IOException e) {
                e.printStackTrace();
            }
        }
    }
}
